﻿using NRSS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NRss
{
    public partial class Login_Form : Form
    {
        private static readonly object Locker = new object();
        private static int _sn = 0;
        private static string username;

        public static string Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }

        public Login_Form()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Settings_Form sf = new Settings_Form();
            sf.ShowDialog();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            ErrorProvider errInfo = new ErrorProvider();
            errInfo.Clear();

            string pwd = CrytMd5.GetMd5Str(this.txt_PassWord.Text.Trim());

            if (string.IsNullOrEmpty(txt_UserName.Text.Trim()))
            {
                try
                {
                    errInfo.SetError(txt_UserName, "用户编码不能为空！");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            if (String.IsNullOrEmpty(txt_PassWord.Text.Trim()))
            {
                try
                {
                    errInfo.SetError(txt_PassWord, "用户密码不能为空！");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                finally
                {
                }
            }
            else
            {
                try
                {
             
              

                    int i = Convert.ToInt32(SQLHelper2.ExecuteScalar("select count( *) from Operators where curr_user_id='" + txt_UserName.Text + "' and password='" + txt_PassWord.Text + "'", null));
                    if (i > 0)
                    {

                        DataTable dt_role = SQLHelper2.ExecuteDataTable("select Role_code from operators where curr_user_name='" + txt_UserName.Text + "' ");

                        DataTable dtb_user_right = SQLHelper2.ExecuteDataTable("select module_code from user_right where role_code='" + dt_role.Rows[0]["Role_code"].ToString() + "' group by module_code");
                        int count = dtb_user_right.Rows.Count;
                        if (count == 1)
                        {
                            string module = dtb_user_right.Rows[0]["module_code"].ToString().Trim();
                            switch (module)
                            {
                                case "I01":
                                    PO_order p = new PO_order();
                                    p.ShowDialog();
                                    this.Hide();
                                    break;
                                case "I02":
                                    Purchasing p1 = new Purchasing();
                                    p1.ShowDialog();
                                    this.Hide();
                                    break;
                                default:
                                    this.Hide();
                                    Main2 m = new Main2();
                                    username = txt_UserName.Text;
                                    m.Flag = txt_UserName.Text;
                                    m.ShowDialog();
                                    Application.Exit();
                                    break;
                                   
                            }
                        }
                        else
                        {
                            this.Hide();
                            Main2 m = new Main2();
                            username = txt_UserName.Text;
                            m.Flag = txt_UserName.Text;
                            m.ShowDialog();
                            Application.Exit();
                        }
                    }
                    else
                    {
                        MessageBox.Show("用户编码或用户密码不正确！", "软件提示");
                        txt_PassWord.Text = null;
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;


                }
            }
        }

        private void Login_Form_Activated(object sender, EventArgs e)
        {
            this.txt_UserName.Focus();
        }

        private void Login_Form_Load(object sender, EventArgs e)
        {


          
            this.MaximizeBox = false;//
        }

        private void btn_Login_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txt_PassWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1_Click(sender, e);
            }
        }

        private void btn_Cloes_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public static string GenerateId()
        {
            lock (Locker)  //lock 关键字可确保当一个线程位于代码的临界区时，另一个线程不会进入该临界区。 
            {
                if (_sn == int.MaxValue)
                {
                    _sn = 0;
                }
                else
                {
                    _sn++;
                }

                Thread.Sleep(100);

                return  DateTime.Now.ToString("yyyyMMdd") + _sn.ToString().PadLeft(4, '0');
            }
        }
    }
}

  
  
   /// <summary> 
   /// 生成订单编号 
   /// </summary> 
   /// <returns></returns> 
   
 

